package com.ynny.bookmanagersystem.controller;

import com.ynny.bookmanagersystem.dao.UserDao;
import com.ynny.bookmanagersystem.dao.UserDaoImpl;
import com.ynny.bookmanagersystem.entity.User;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

@WebServlet("/qwe/select")
public class SelectUserServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request,
                           HttpServletResponse response) throws ServletException, IOException {

        String userName = request.getParameter("username");

        String name = request.getParameter("name");

        UserDao userDao = new UserDaoImpl();

        List<User> users = userDao.selectUsersByUserNameAndName(userName, name);


        if (users != null && users.size() > 0) {

            //重定向 list.jsp

            request.getSession().setAttribute("users", users);
            response.sendRedirect("/user/list.jsp");


        }else {
           //保存一个 提示信息
            request.setAttribute("info", "没有查询到用户信息");
            //转发到 login.jsp
            request.getRequestDispatcher("/user/list.jsp").forward(request, response);

        }

    }
}
